package shujia.day14;

import shujia.day14.entity.User;

import java.io.*;
import java.util.*;

/*
    登录注册第二个版本，用户的信息，使用类的对象表示，加入邮箱，手机号，家庭住址信息
 */
public class LoginDemo2 {
    private static final List<User> ALL_USERS = getAllUsers();

    public static void main(String[] args) {
        init();
    }

    public static void init() {
        System.out.println("=================== 欢迎登录张成阳婚姻介绍所 ====================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要做的操作：(1.登录 2.注册)");
        if (sc.hasNextInt()) {
            switch (sc.nextInt()) {
                case 1:
                    System.out.println();
                    System.out.println();
                    login();
                    break;
                case 2:
                    System.out.println();
                    System.out.println();
                    register();
                    break;
                default:
                    System.out.println();
                    System.out.println();
                    System.out.println("没有该选项！");
                    break;
            }
        } else {
            System.out.println("您输入的类型有误！");
        }

    }

    public static void login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎登录！");
        System.out.println("请输入您的用户名：");
        String name = sc.next();

        User u = null;
        for (User user : ALL_USERS) {
            if (name.equals(user.getName())) {
                u = user;
                break;
            }
        }

        // 从map集合中判断用户是否存在
        if (u!=null) {
            System.out.println("请输入用户的密码：");
            String pwd = sc.next();
            if (pwd.equals(u.getPwd())) {
                System.out.println("登录成功！！");
            } else {
                System.out.println("登录失败！密码不正确！");
            }
        } else {
            System.out.println("该用户还未注册！");
        }
    }

    public static void register() {
        BufferedWriter bw = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("欢迎注册！");
            System.out.println("请设置您的用户名：");
            String name = sc.next();
            User u = null;
            for (User user : ALL_USERS) {
                if (name.equals(user.getName())) {
                    u = user;
                    break;
                }
            }

            // 从map集合中判断用户是否存在
            if (u!=null) {
                System.out.println("该用户名已被使用！！");
            } else {
                System.out.println("请设置您的账户密码：");
                String pwd = sc.next();
                System.out.println("请设置您的账户邮箱：");
                String email = sc.next();
                System.out.println("请设置您的账户手机号：");
                String phoneNum = sc.next();
                System.out.println("请设置您的账户地址：");
                String address = sc.next();
                //将用户名和密码写入到文件中【追加写】
                bw = new BufferedWriter(new FileWriter("src/shujia/day14/users.txt", true));
                bw.write(name + "|" + pwd+"|"+email+"|"+phoneNum+"|"+address);
                bw.newLine();
                bw.flush();

                System.out.println("用户注册成功！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<User> getAllUsers() {
        BufferedReader br = null;
        ArrayList<User> userList = new ArrayList<>();
        try {
            //读取用户文件，将所有用户封装到Map集合中
            br = new BufferedReader(new FileReader("src/shujia/day14/users.txt"));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] infoArr = line.split("\\|");
                String name = infoArr[0];
                String password = infoArr[1];
                String email = infoArr[2];
                String phoneNum = infoArr[3];
                String address = infoArr[4];

                User user = new User(name, password, email, phoneNum, address);
                userList.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return userList;

    }

}
